/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.web.elmt;

import javax.servlet.http.*;

import org.sng.util.var.*;

public class NodeButton extends Node {
	
	public NodeButton()
	{
		super();
	}
	
	public NodeButton(int n)
	{
		super(n);
	}
	
	public NodeButton(String id)
	{
		super();
		
		this.PushProperty("id", id);
	}
	
	public NodeButton(int n, String id)
	{
		super(n);
		
		this.PushProperty("id", id);
	}
	
	public NodeButton Create()
	{
		return new NodeButton();
	}
	
	public NodeButton Create(int n)
	{
		return new NodeButton(n);
	}
	
	public NodeButton Create(String id, RuntimeEnvironment re)
	{
		HttpServletRequest request = re.GetRequest();
		
		NodeButton o = new NodeButton();
		
		o.m_properties.put("id", new SString(id));
		
		o.m_properties.put("value", new SString(request.getParameter(id + "_value")));

		String postback = request.getParameter(id + "_postback");
		if (postback != null)
			o.PushProperty("postback", postback);
		
		return o;
	}
	
	public String GetType()
	{
		return "button";
	}
	
	public StringBuffer Do(RuntimeEnvironment re) throws Exception
	{
		StringBuffer result = new StringBuffer();
		
		SVoid prop = null;
		
		if ((prop = this.m_properties.get("id")) == null)
		{
			throw new Exception("Critical property \"id\" is missing.");
		}
		
		String sid = prop.GetString(re);
		
		String value;
		if ((prop = this.m_properties.get("value")) == null) {
			value = "";
		}
		else {
			value = prop.GetString(re);
		}
		
		String sync, disable;
		if ((prop = this.m_properties.get("sync")) == null) {
			sync = "process";
			disable = "";
		}
		else if (prop.GetString(re).compareTo("false") == 0) {
			sync = "processNonSync";
			disable = "";
		}
		else if (prop.GetString(re).compareTo("half") == 0) {
			sync = "processHalfSync";
			disable = "$(\"" + sid + "\").disabled=!$(\"" + sid + "\").disabled;";
		}
		else {
			sync = "process";
			disable = "";
		}
		
		String callback;
		if ((prop = this.m_properties.get("callback")) == null) {
			callback = "";
		}
		else {
			callback = "&callback=" + prop.GetString(re);
		}
		
		StringBuffer properties = new StringBuffer();
		if ((prop = this.m_properties.get("class")) != null) {
			properties.append(" class=\"");
			properties.append(prop.GetString(re));
			properties.append("\"");
		}
		if ((prop = this.m_properties.get("style")) != null) {
			properties.append(" style=\"");
			properties.append(prop.GetString(re));
			properties.append("\"");
		}
		
		result.append("<input id=\"" + sid + "\" type=\"button\" value=\"" + value + "\"" + properties + " onclick=\"javascript:on" + sid + "Click();\" />");
		
		re.AppendJavascript("var on" + sid + "Click_Finished=function(){$(\"" + sid + "\").disabled=!$(\"" + sid + "\").disabled;};");
		re.AppendJavascript("var on" + sid + "Click=function(){var para=GetPostList();"+ disable + sync + "(g_processUrl+\"?id=" + sid + "&event=Click" + callback + "\",para,on" + sid + "Click_Finished,null);};");
		
		return result;
	}
	
	public void AppendPostList(RuntimeEnvironment re) {
		String sid = this.m_properties.get("id").GetString(re);
		
		re.AppendPostList("if($(\"" + sid + "\")!=null){");
		re.AppendPostList("para.add(\"cid\", \"" + sid + "\");");
		re.AppendPostList("para.add(\"type\", \"Button\");");
		re.AppendPostList("para.add(\"" + sid + "_value\", $(\"" + sid + "\").value);");
		
		SVoid postback = this.GetProperty("postback");
		if (postback != null)
			re.AppendPostList(sid + "_postback", "\"" + SString.ToStringLiterealSafe(postback.GetString(re)) + "\"");
		
		re.AppendPostList("};");
	}
	
	public NodeButton SwitchDisable(RuntimeEnvironment re) {
		String sid = this.GetProperty("id").GetString(re);
		
		re.AppendJavascript("$(\"" + sid + "\").disabled=!$(\"" + sid + "\").disabled;");
		
		return this;
	}

}
